<template>
  <router-view></router-view>
</template>

<script setup lang="ts">
import { onMounted, watch } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { useChatStore } from '@/stores/chat'
import { useRoute } from 'vue-router'

const authStore = useAuthStore()
const chatStore = useChatStore()
const route = useRoute()

onMounted(() => {
  // 初始化认证状态
  authStore.initialize()
})

// 监听路由变化
watch(
  () => route.path,
  async (path) => {
    // 只在进入聊天页面且已登录时初始化聊天服务
    if (path.startsWith('/chat') && authStore.token) {
      console.log('Initializing chat service with token:', !!authStore.token)
      
      // 添加小延迟确保 token 已保存
      await new Promise(resolve => setTimeout(resolve, 100))
      
      try {
        await chatStore.initialize()
      } catch (error) {
        console.error('Failed to initialize chat service:', error)
      }
    }
  }
)
</script> 